/* $Revision: $ $Date: $
 * 
 * Copyright (C) 2009 Robert Stephan
 * 
 * This file is part of *** CaCaO - Card Catalogs Online ***
 *
 * CaCaO is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CaCao is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cacao.util;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import cacao.server.CaCaOServer;


//Nutzer anlegen:
//mysql> GRANT ALL PRIVILEGES ON cacao.* to 'cacao'@'%' IDENTIFIED BY '***';


public class InitializeDB {
	// "static" removed to avoid accidental starts
	public void main(String[] args){
		Properties ipacProps = CaCaOServer.getInstance().getProperties();
		try {
			Class.forName(ipacProps.getProperty("CaCaO.server.db.classname")).newInstance();
			Connection conn = CaCaOServer.getInstance().getDBConnection();
			conn.setAutoCommit(false);
			Statement s = conn.createStatement();
			s.execute("DROP TABLE IF EXISTS catalogs");
			s.execute(ipacProps.getProperty("CaCaO.server.db.create.catalogs"));

			s.execute("DROP TABLE IF EXISTS catalogindex");
			s.execute(ipacProps.getProperty("CaCaO.server.db.create.catalogindex"));

			s.execute("DROP TABLE IF EXISTS items");
			s.execute(ipacProps.getProperty("CaCaO.server.db.create.items"));

			s.close();
			conn.commit();
			conn.close();	            

			System.out.println("Database tables successfully created!");
		} catch (SQLException ex) {
			// Fehler behandeln
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
